package bbdd;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BDSocio {

	
	private static Statement st;
	private static Connection c;
	private static ResultSet rs;
	
	
	
	public static void insertarSocio(String dni,String nom,String apell,String dir,BaseDatos bbdd){
		
		String cadena="INSERT into Socio VALUES('" + dni +"','"+ nom + "','" + apell + "','" + dir + " ')";
		
		bbdd.abrir();
		try{
			c=bbdd.getConexion();
			st=c.createStatement();
			st.executeUpdate(cadena);
			st.close();				
			
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
		bbdd.cerrar();
		
	}
	
	public static void actualizarSocio(String dir,String dni,BaseDatos bbdd){
		
		String cadena="UPDATE Socio set direccion=' "+ dir +"'where dni='"+dni;
		bbdd.abrir();
		try{
			c=bbdd.getConexion();
			st=c.createStatement();
			st.executeUpdate(cadena);
			st.close();
			
			
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
		bbdd.cerrar();
		
	}
	
	
	public static void eliminarSocio(String dni,BaseDatos bbdd){
		
		String cadena="DELETE FROM Socio WHERE dni=' "+dni+"'";
		bbdd.abrir();
		try{
			c=bbdd.getConexion();
			st=c.createStatement();
			st.executeUpdate(cadena);
			st.close();		
			
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
		bbdd.cerrar();
	}
	
	
	public static String selecionSocio (String dni,BaseDatos bbdd){
		
		String cadena="SELECT nombre,apellidos,direccion FROM Socio WHERE dni='"+ dni +"'";
		bbdd.abrir();
		try{
			c=bbdd.getConexion();
			st=c.createStatement();
			rs=st.executeQuery(cadena);
			String result="";
			while(rs.next())
			{
				
				String t=rs.getString(1)+""+rs.getString(2)+""+rs.getString(3)+"\n";
				result+=t;
			}
			st.close();
			bbdd.cerrar();
			return result;		
			
			
			
		}catch(SQLException e){
			System.out.println(e.getMessage());
		}
		bbdd.cerrar();
		return null;
		
		
	}
	
	
}
